package com.auxgroup.hr.front.service.thread;

import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.auxgroup.hr.front.commons.Constant;
import com.auxgroup.hr.front.commons.config.BusinessConfig;
import com.auxgroup.hr.front.commons.utils.FtpFileUtil;
import com.auxgroup.hr.front.commons.vo.WfFileRespVo;
import com.auxgroup.hr.front.dao.domain.PsCModAttT;
import com.auxgroup.hr.front.dao.domain.PsCWfFileAtt;

/** 
 * @author lvliuhao 
 * @version 创建时间：2018年8月9日 下午4:11:36 
 * 类说明 
 */
public class DownWfFileThread implements Runnable {

	private FtpFileUtil ftpFileUtil;
		
	private PsCModAttT wfFile;
	
	private String ftpPath;
	
	public List<WfFileRespVo> respVOs;
	
	private BusinessConfig businessConfig;
	
	
    /**
     * 需要下载的文件类型
     */
    List<String> needDownLoadFileType = new ArrayList<String>(Arrays.asList("doc","docx","xls","xlsx","ppt","pptx"));

	
	public DownWfFileThread(FtpFileUtil ftpFileUtil,String ftpPath,PsCModAttT wfFile,
			List<WfFileRespVo> respVOs,BusinessConfig businessConfig){
		this.ftpFileUtil = ftpFileUtil;
		this.ftpPath = ftpPath;
		this.wfFile = wfFile;
		this.respVOs = respVOs;
		this.businessConfig = businessConfig;
	}

	/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		WfFileRespVo WfFileRespVo = new WfFileRespVo();
		String fileType = wfFile.getAttachsysfilename().substring(wfFile.getAttachsysfilename().lastIndexOf(".")+1, wfFile.getAttachsysfilename().length());
		if(!isFileExist(wfFile,WfFileRespVo,fileType)){
			//从ftp下载文件到服务器
			if(ftpFileUtil.downloadFile(ftpPath, wfFile.getAttachsysfilename(), businessConfig.getFtpFileResourceLocation(),ftpFileUtil.getNewFtpClient())){
				WfFileRespVo.setFileType(fileType);
				WfFileRespVo.setFileSysName(wfFile.getAttachsysfilename());
				//设置文件路径
				setFileUrl(WfFileRespVo,wfFile.getcTransactionNbr(),wfFile.getSeqnbr(),fileType);
				WfFileRespVo.setOriginalName(wfFile.getAttachuserfile());
				File localFile = new File(businessConfig.getFtpFileResourceLocation() + "/" + wfFile.getAttachsysfilename()); 
				if(localFile.exists()&&localFile.isFile()){
					WfFileRespVo.setFileSize(localFile.length()/1024);
				}
				respVOs.add(WfFileRespVo);
			}
		}else{
			respVOs.add(WfFileRespVo);
		}
	}
	
	/**
	 * 判断 文件是否已经存在， 如果存在就设置respvo的值
	 * @param wfFile
	 * @param respVo
	 * @param fileType
	 * @return
	 */
	private Boolean isFileExist(PsCModAttT wfFile,WfFileRespVo respVo,String fileType){
		File file = new File(businessConfig.getFtpFileResourceLocation()+"/"+wfFile.getAttachsysfilename());
		if(file.exists()&&file.isFile()){
			respVo.setFileType(fileType);
			respVo.setFileSysName(wfFile.getAttachsysfilename());
			//设置文件路径
			setFileUrl(respVo,wfFile.getcTransactionNbr(),wfFile.getSeqnbr(),fileType);
			respVo.setOriginalName(wfFile.getAttachuserfile());
			respVo.setFileSize(file.length()/1024);
			return true;
		}
		return false;
	}
	
	/**
	 * 根据不同的文件类型设置不同的文件访问地址
	 * @param respVo
	 * @param transNum
	 * @param seqnbr
	 * @param fileType
	 */
	private void setFileUrl(WfFileRespVo respVo,String transNum,BigDecimal seqnbr,String fileType){
		StringBuilder ftpFileUrl = new StringBuilder(Constant.FTPFILE_STATIC_PATH).append(respVo.getFileSysName());
		if(needDownLoadFileType.contains(fileType)){
			StringBuffer url = new StringBuffer("/download/wfFile?cTransactionNbr=");
			url.append(transNum);
			url.append("&seqnbr=");
			url.append(seqnbr);
			respVo.setFileUrl(url.toString());
		}else{
			respVo.setFileUrl(ftpFileUrl.toString());
		}
		respVo.setAndroidUrl(ftpFileUrl.toString());
	}
	
	
	
}
 